package com.wcj.demo.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class DruidConfig {

    //配置数据源
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean//将返回值载入spring容器
    public DataSource druidDataSource(){
        return new DruidDataSource();
    }

    //配置servlet
    @Bean
    public ServletRegistrationBean servletRegistrationBean(){
        ServletRegistrationBean bean=new ServletRegistrationBean(new StatViewServlet(),"/druid/*");//注意参数
        //配置bean的属性（使用Map打包配置）
        Map<String,String>map=new HashMap<>();
        //设置后台界面用户名/密码
        map.put("loginUsername","wcj");
        map.put("loginPassword","123456");
        map.put("allow","");//设置允许哪些地址访问，默认所有
      //  map.put("deny","192.168.5.52");//设置不能访问的地址
        bean.setInitParameters(map);//装入属性
        return bean;
    }


    //自定义filter过滤器
    @Bean
    public FilterRegistrationBean filterRegistrationBean(){
        FilterRegistrationBean bean=new FilterRegistrationBean();
        //设置过滤器
        bean.setFilter(new WebStatFilter());
        //对所有请求过滤监听
        bean.addUrlPatterns("/*");
        Map<String,String>map=new HashMap<>();
        //静态资源放行exclusions属性
        map.put("exclusions","*.js,*.css,*.jq,*.jpeg,*.gif,*.jpg,*.png");
        bean.setInitParameters(map);
        return bean;
    }



}
